<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;

use App\Models\Article;
use App\Models\ArticleCate;

use App\Http\Resources\V1\ArticleCateResource;
use App\Http\Resources\V1\ArticleResource;

use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;

class ArticlesController extends Controller
{
    public function cate_index(Request $request)
    {
        ArticleCateResource::wrap('data');

        return ArticleCateResource::collection(ArticleCate::all());
    }

    public function index(Request $request)
    {
        $articles = QueryBuilder::for(Article::class)
            ->allowedIncludes('cate')
            ->allowedFilters([
                'title',
                AllowedFilter::exact('article_cate_id'),
                // AllowedFilter::scope('order')->default('recentReplied'),
            ])
            ->recent()
            ->paginate();

        return ArticleResource::collection($articles);
    }

    public function show(Article $article)
    {
        $article->load(['replies.user']);

        return new ArticleResource($article->load(['cate', 'replies.user']));
    }
}
